package com.ubis.ibms.plugin.dialogs;

import java.util.List;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.nebula.widgets.calendarcombo.CalendarCombo;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.ubis.ibms.plugin.common.constants.Constants;
import com.ubis.ibms.plugin.common.verify.LeaseVerify;
import com.ubis.ibms.plugin.common.viewer.ComboLabelProvider;
import com.ubis.ibms.plugin.dao.EstateManager;
import com.ubis.ibms.plugin.dao.LeaseManager;
import com.ubis.ibms.plugin.dao.PropertyManager;
import com.ubis.ibms.plugin.model.BillLog;
import com.ubis.ibms.plugin.model.Building;
import com.ubis.ibms.plugin.model.Floor;
import com.ubis.ibms.plugin.model.Room;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.core.databinding.beans.PojoProperties;
import org.eclipse.core.databinding.UpdateValueStrategy;

public class PropertyAddDialog extends Dialog {
	private DataBindingContext m_bindingContext;
	
	// 楼栋
	private Text buildingText;
	// 楼层
	private Text floorText;
	// 房间
	private Text roomText;

	// 交费起始时间
	private CalendarCombo dateFromCombo;
	// 交费截止时间
	private CalendarCombo dateToCombo;
	// 交费时长 取3个月或6个月
	private int duration;
	private Button threeButton;
	private Button sixButton;
	// 计月方式 ture为自然月 false为按日期推算
	private boolean monthMode;
	private Button natureButton;
	private Button calcButton;
	// 提醒日期
	private CalendarCombo remindCombo;
	// 交费金额
	private Text totalText;
	// 交费按钮
	private Button payButton;

	private Long room_moi_id;
	private BillLog billLog;
	private String remindDate;
	
	private Composite container;
	
	private Room room;
		
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public PropertyAddDialog(Shell parentShell, Room room) {
		super(parentShell);
		this.room = room;
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		container = (Composite) super.createDialogArea(parent);
		container.setLayout(new GridLayout(6, false));

		// 楼栋
		Label buildingLabel = new Label(container, SWT.RIGHT);
		buildingLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		buildingLabel.setText("楼栋：");
		buildingText = new Text(container, SWT.BORDER);
		buildingText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		buildingText.setEditable(false);

		// 楼层
		Label floorLabel = new Label(container, SWT.RIGHT);
		floorLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		floorLabel.setText("楼层：");
		floorText = new Text(container, SWT.BORDER);
		floorText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		floorText.setEditable(false);

		// 房间号
		Label roomLabel = new Label(container, SWT.RIGHT);
		roomLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		roomLabel.setText("房间：");
		roomText = new Text(container, SWT.BORDER);
		roomText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		roomText.setEditable(false);

		// 交费时长
		Label durationLabel = new Label(container, SWT.RIGHT);
		durationLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		durationLabel.setText("交费时长：");
		Composite durationComposite = new Composite(container, SWT.NONE);
		durationComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
				true, false, 2, 1));
		durationComposite.setLayout(new GridLayout(2, false));
		threeButton = new Button(durationComposite, SWT.RADIO);
		threeButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String type = null;
				if (natureButton.getSelection()) {
					type = Constants.NATURE;
				}
				if (calcButton.getSelection()) {
					type = Constants.CALCULATE;
				}

				billLogGenerate(room_moi_id, dateFromCombo, 3,
						type);
			}
		});
		threeButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true,
				false, 1, 1));
		threeButton.setText("3个月");
		threeButton.setSelection(true);

		sixButton = new Button(durationComposite, SWT.RADIO);
		sixButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true,
				false, 1, 1));
		sixButton.setText("6个月");
		sixButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String type = null;
				if (natureButton.getSelection()) {
					type = Constants.NATURE;
				}
				if (calcButton.getSelection()) {
					type = Constants.CALCULATE;
				}
				billLogGenerate(room_moi_id, dateFromCombo, 6,
						type);
			}
		});

		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		// 计月方式
		Label monthModeLabel = new Label(container, SWT.RIGHT);
		monthModeLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		monthModeLabel.setText("计月方式：");
		Composite modeComposite = new Composite(container, SWT.NONE);
		modeComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 2, 1));
		modeComposite.setLayout(new GridLayout(2, false));
		natureButton = new Button(modeComposite, SWT.RADIO);
		natureButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true,
				false, 1, 1));
		natureButton.setText("自然月");
		natureButton.setSelection(true);
		natureButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int mon = 0;
				if (threeButton.getSelection()) {
					mon = 3;
				}
				if (sixButton.getSelection()) {
					mon = 6;
				}
				billLogGenerate(room_moi_id, dateFromCombo, mon,
						Constants.NATURE);
			}
		});

		calcButton = new Button(modeComposite, SWT.RADIO);
		calcButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true,
				false, 1, 1));
		calcButton.setText("推算");
		calcButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int mon = 0;
				if (threeButton.getSelection()) {
					mon = 3;
				}
				if (sixButton.getSelection()) {
					mon = 6;
				}

				billLogGenerate(room_moi_id, dateFromCombo, mon,
						Constants.CALCULATE);
			}
		});
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);

		// 起始日期
		Label dateFromLabel = new Label(container, SWT.RIGHT);
		dateFromLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		dateFromLabel.setText("起始日期：");
		dateFromCombo = new CalendarCombo(container, SWT.BORDER);
		dateFromCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));

		// 截止日期
		Label dateToLabel = new Label(container, SWT.RIGHT);
		dateToLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		dateToLabel.setText("截止时间：");
		dateToCombo = new CalendarCombo(container, SWT.BORDER);
		dateToCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		dateToCombo.setEnabled(false);

		// 提醒日期
		Label remindLabel = new Label(container, SWT.RIGHT);
		remindLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		remindLabel.setText("提醒时间：");
		remindCombo = new CalendarCombo(container, SWT.BORDER);
		remindCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));

		// 交费金额
		Label totalLabel1 = new Label(container, SWT.RIGHT);
		totalLabel1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		totalLabel1.setText("交费金额：");
		totalText = new Text(container, SWT.BORDER);
		totalText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		totalText.setEditable(false);
		Label totalLabel2 = new Label(container, SWT.RIGHT);
		totalLabel2.setText("元");
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		new Label(container, SWT.NONE);
		
		updateInfo();

		return container;
	}
	
	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
				false, 1, 1));
		parent.setLayout(new GridLayout(2, false));
		payButton = createButton(parent, IDialogConstants.CLIENT_ID, "确认交费",
				false);
		payButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true,
				false, 1, 1));
		payButton.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				// 点击时
				// 选中threeButton时交费时长设置为3个月，否则是6个月
				duration = threeButton.getSelection() ? 3 : 6;
				// 选中natureButton时 计月方式设置为自然月，否则为推算
				monthMode = natureButton.getSelection();
				// TODO
				// 添加业主的充值记录
				// 添加房间的物业费消费记录
				// System.out.println("------"+remindCombo.getDateAsString());
				if (billLog != null && billLog.getBill_point() != 0.0) {
					if(PropertyManager.saveOneRoomLogs(billLog)){
						MessageDialog.openInformation(PropertyAddDialog.this.getShell(), "通知",
								"物业费缴费成功！");
					}else {
						MessageDialog.openInformation(PropertyAddDialog.this.getShell(), "通知",
								"添加物业费存储过程出错，物业费缴费失败！");
					}
				} else {
					MessageDialog.openInformation(PropertyAddDialog.this.getShell(), "错误",
							"错误！");
				}
				System.out.println("payButton mouse up.");
			}
		});
		m_bindingContext = initDataBindings();
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 300);
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("交物业费");
	}

	public void billLogGenerate(Long room_moi_id,
			CalendarCombo begin, int mon, String type) {
		if (begin.getDateAsString() != null
				&& begin.getDateAsString().length() > 0 && room_moi_id!=null) {
			billLog = PropertyManager.showProperty(room_moi_id,
					begin.getDateAsString(), mon, type);
			if (billLog != null) {
				dateToCombo.setText(billLog.getNowvalue());
				totalText.setText(Double.toString(billLog.getBill_point()));
				remindCombo.setText(PropertyManager.getRemindDateString(billLog.getNowvalue()));
			}
		}
	}
	
	private void updateInfo(){
		room_moi_id = room.getId();
		if (room_moi_id != null) {
			String m = PropertyManager
					.getBeginDateString(room_moi_id);
			if (m != null) {
				if (m.contains(" 00:00:00")) {
					m = m.split(" ")[0];
				}
				dateFromCombo.setText(m);
				String type = null;
				int mon = 0;
				type = natureButton.getSelection() ? Constants.NATURE
						: Constants.CALCULATE;
				mon = sixButton.getSelection() ? 6 : 3;

				billLogGenerate(room_moi_id, dateFromCombo, mon, type);
				if (billLog == null) {
					MessageDialog.openInformation(PropertyAddDialog.this.getShell(), "通知",
							"由于无法找到业主名称，无法生成物业费账单！");
				}
			} else {
				dateFromCombo.setText("");
			}
		}
	}

	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		IObservableValue observeTextBuildingTextObserveWidget = WidgetProperties.text(SWT.Modify).observe(buildingText);
		IObservableValue buildingIdRoomObserveValue = PojoProperties.value("buildingId").observe(room);
		bindingContext.bindValue(observeTextBuildingTextObserveWidget, buildingIdRoomObserveValue, new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), null);
		//
		IObservableValue observeTextFloorTextObserveWidget = WidgetProperties.text(SWT.Modify).observe(floorText);
		IObservableValue floorIdRoomObserveValue = PojoProperties.value("floorId").observe(room);
		bindingContext.bindValue(observeTextFloorTextObserveWidget, floorIdRoomObserveValue, new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), null);
		//
		IObservableValue observeTextRoomTextObserveWidget = WidgetProperties.text(SWT.Modify).observe(roomText);
		IObservableValue ems_idRoomObserveValue = PojoProperties.value("ems_id").observe(room);
		bindingContext.bindValue(observeTextRoomTextObserveWidget, ems_idRoomObserveValue, new UpdateValueStrategy(UpdateValueStrategy.POLICY_NEVER), null);
		//
		return bindingContext;
	}
}
